<?php

require_once("config.php");
require_once("nusoap/nusoap.php");
require_once("utils.php");
require_once("Component.php");

class Lead extends Component {

	var $defaultColumns;
	var $columnMappings;
		
        function __construct($proxy){
		
			$this->defaultColumns = array(
					'lead_source',
					'campaign_id',
					'lead_source_description',
					'refered_by',
					'salutation',
					'first_name',
					'last_name',
					'account_name',
					'title',
					'department',
					'do_not_call',
					'assigned_user_id',
					'status',
					'status_description',
					'phone_work',
					'phone_mobile',
					'phone_home',
					'phone_other',
					'phone_fax',
					'email1',
					'primary_address_street',
					'primary_address_city',
					'primary_address_state',
					'primary_address_postalcode',
					'primary_address_country',
					'alt_address_street',
					'alt_address_city',
					'alt_address_state',
					'alt_address_postalcode',
					'alt_address_country',
					'description'
        		);

			$this->columnMappings = array ();

		parent::__construct("Leads", "leads", $proxy);
			
        }


	function getRecords(){

                $customQuery = null;

                if ($this->keysOnly == null) {

                        $query = $this->getQueryColumns($this->defaultColumns);
                        $query = str_replace('leads.email1','email_address email1', $query);
                        $query = 'select '.$query.' from leads ';
                        $query .=  ' LEFT JOIN  email_addr_bean_rel on leads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Leads\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                        $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
			$account_name = isset($_REQUEST['account_name']) ? $_REQUEST['account_name'] : null;
			$account_name = str_replace('"',"",$account_name);
			$customQuery = ($account_name != null) ? " leads.account_name=".$this->quote.$account_name.$this->quote : null;
                } else {
                        $query = "select id from leads";
                }

                return $this->getRows($query, $customQuery, " leads.date_modified desc ", $this->columnMappings);

        }

	function getRecord($condition){

		
		$query =  $this->getQueryColumns($this->defaultColumns);
                $query = str_replace('leads.email1','email_address email1', $query);
                $query = 'select '.$query.' from leads ';
                $query .=  ' LEFT JOIN  email_addr_bean_rel on leads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Leads\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
		$query .=  ' WHERE '.$condition;

                return $this->getRow($query);
        }


        function getRecordCount() {

                $query = 'select count(*) row_count from leads ';
		$query .=  ' LEFT JOIN  email_addr_bean_rel on leads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Leads\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;

		$account_name = isset($_REQUEST['account_name']) ? $_REQUEST['account_name'] : null;
                $customQuery = ($account_name != null) ? " leads.account_name=".$this->quote.$account_name.$this->quote : null;

                $ret = $this->getTotalRowCount($query, $customQuery, $this->columnMappings);
		return $ret;
        }


				
				
	function saveRecord($id, $lead_source, $campaign_id, $lead_source_description, $refered_by,
							$salutation, $first_name, $last_name, $account_name, $title,
							$department, $do_not_call, $assigned_user_id, $status, $status_description,
							$phone_work, $phone_mobile, $phone_home, $phone_other, $phone_fax, $email1, 
							$primary_address_street, $primary_address_city, $primary_address_state, 
							$primary_address_postalcode, $primary_address_country, $alt_address_street, 
							$alt_address_city, $alt_address_state, $alt_address_postalcode, $alt_address_country, 
							$description, $date_modified) {
		
			if ($id) {
				$response = $this->proxy->set_entry($this->sess, 'Leads',
								array(array("name" => "id", "value" => $id),
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description)
								)
							);
			} else {
				$response = $this->proxy->set_entry($this->sess, 'Leads',
								array(
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description)
								)
							);
            }			
			return $response;
		}
		
		function deleteLead($id) {
		
			$response = $this->proxy->set_entry($this->sess, 'Leads', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'deleted',"value" => 1),
            						 )
            				);
		
			return $response;
		
		}
		
	}
	
$component = new Lead($proxy);

					
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
					
$statusArray = array ("" => "",
					"New" => "New",
					"Assigned" => "Assigned",
					"In Process" => "In Process",
					"Converted" => "Converted",
					"Recycled" => "Recycled",
					"Dead" => "Dead");
					
$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");

?>
